#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using Gizmox.WebGUI;
using Premiere.Framework;

#endregion

namespace PremiereMX
{
    public partial class PartPriceInformationListForm : Form
    {
        PartDTO m_part;
        bool m_readOnly = false;
        IList m_currencyList = null;
        IList m_priceCategoryList = null;
        public PartPriceInformationListForm(PartDTO part, bool readOnly)
        {
            InitializeComponent();
            m_part = part;
            m_currencyList = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            m_priceCategoryList = ServiceRegistry.GetInstance().GetPriceCategoryService().LoadAll();
            foreach (object key in m_part.SellingPriceInformation.Keys)
            {
                PartSellingPriceInformationDTO sp = (PartSellingPriceInformationDTO)m_part.SellingPriceInformation[key];
                addInformation(sp);
            }
            cBottomPrice.ReadOnly = readOnly;
            cCostPrice.ReadOnly = readOnly;
            cCurrency.ReadOnly = readOnly;
            cDiscPrecent.ReadOnly = readOnly;
            cDiscPrice.ReadOnly = readOnly;
            cPriceCategory.ReadOnly = readOnly;
            cUnit.ReadOnly = readOnly;
            cPriceCategory.ReadOnly = readOnly;
            cSellingPrice.ReadOnly = readOnly;

            cPriceCategory.DataSource = Utils.GetListCode(m_priceCategoryList);
            cUnit.DataSource = Utils.GetListCode(m_part.GetAllUoM());
            cCurrency.DataSource = Utils.GetListCode(m_currencyList);

            gridInformation.CellValidating += new DataGridViewCellValidatingEventHandler(gridInformation_CellValidating);
        }

        void gridInformation_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int testint;
            if ((e.ColumnIndex == cSellingPrice.Index) || 
                (e.ColumnIndex == cCostPrice.Index) ||
                (e.ColumnIndex == cBottomPrice.Index) ||
                (e.ColumnIndex == cDiscPrecent.Index) ||
                (e.ColumnIndex == cDiscPrice.Index)           
                )
            {
                if (!CommonUtils.TryParse(e.FormattedValue.ToString(), out testint))
                {
                    gridInformation.Rows[e.RowIndex].ErrorText =
                        "Error: Invalid entry";
                    e.Cancel = true;
                }
            }
        }

        private void addInformation(PartSellingPriceInformationDTO sp)
        {
            gridInformation.Rows.Add(
                sp.PriceCategory.Code,
                sp.Currency.Code,
                sp.Unit.Code,
                sp.SellingPrice.ToString(),
                sp.CostPrice.ToString(),
            sp.BottomPrice.ToString(),
            sp.DiscountAmount.ToString(),
            sp.DiscountPercent.ToString()
            );
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void wizardButton_Click(object sender, EventArgs e)
        {
            try
            {
                m_part.ClearSellingPrices();
                IList informationList = getListInformation();
                foreach (PartSellingPriceInformationDTO pspi in informationList)
                {
                    m_part.AddSellingPriceInformation(pspi);
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        private IList getListInformation()
        {
            IList result = new ArrayList();
            for (int i = 0; i < gridInformation.Rows.Count; i++)
            {
                PartSellingPriceInformationDTO pspi = (PartSellingPriceInformationDTO)gridInformation.Rows[i].Tag;
                if (pspi == null)
                    pspi = new PartSellingPriceInformationDTO();
                if (gridInformation[cCurrency.Index, i].Value == null) continue;
                if (gridInformation[cPriceCategory.Index, i].Value == null) continue;
                if (gridInformation[cUnit.Index, i].Value == null) continue;
                pspi.PriceCategory = (PriceCategoryDTO)Utils.FindDTOInList(gridInformation[cPriceCategory.Index, i].Value.ToString(), m_priceCategoryList);
                pspi.Currency = (CurrencyDTO)Utils.FindDTOInList(gridInformation[cCurrency.Index, i].Value.ToString(),m_currencyList);
                pspi.Unit = (UnitDTO)Utils.FindDTOInList(gridInformation[cUnit.Index, i].Value.ToString(),m_part.GetAllUoM());
                pspi.SellingPrice = Convert.ToDecimal(gridInformation[cSellingPrice.Index, i].Value);
                pspi.CostPrice = Convert.ToDecimal(gridInformation[cCostPrice.Index, i].Value);
                pspi.BottomPrice = Convert.ToDecimal(gridInformation[cBottomPrice.Index, i].Value);
                pspi.DiscountAmount = Convert.ToDecimal(gridInformation[cDiscPrice.Index, i].Value);
                pspi.DiscountPercent = Convert.ToDecimal(gridInformation[cDiscPrecent.Index, i].Value);
                result.Add(pspi);
            }
            return result;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            gridInformation.Rows.Remove(gridInformation.CurrentRow);
        }
       
    }
}